// index.js
// 获取应用实例
const app = getApp()
var utils = require("../../utils/network.js")
Page({
  data: {
    articleList:[],
    categoryList:[],
    hidden: true,                                                                        //隐藏表单控件
    hasMoreData: true,
    total:""
  },

  // 事件处理函数
 getResults:function(){
   let that=this
   wx.request({
     url: 'https://www.tianba.work/api/category/list',
     success(res){
    console.log('分类',res)
    that.setData({
      categoryList:res.data.data.categoryList,
    })
     }
   })
   utils.request({
    url: 'https://www.tianba.work/api/article/list/',
     success: function(res) {
      console.log(res.data)
          that.setData({
            articleList : res.data.data.articleList,
            page:1,
            total:res.data.data.total,
            
          })
          that.onReachBottom()
      }
     
  })
   
   
 },
 onReachBottom: function (){
  var that = this;
    wx.showLoading({
      title: '玩命加载中',
    })
  that.data.page = that.data.page+1;
  utils.request({
    url: 'https://www.tianba.work/api/article/list',
     method: 'GET',
     header: {'Content-Type': 'application/json' },
     data: {page:that.data.page},
     success: function(res) {
      console.log(1111,res)
      const oldDate = that.data.articleList;
       if(res.data.status==0){
          that.setData({
            articleList : oldDate.concat(res.data.data.articleList),
          })
          wx.hideLoading();
       }
       console.log(that.data.total)
       console.log(that.data.articleList.length)
        if(that.data.total==that.data.articleList.length){
          console.log(333)
          wx.showToast({
            title: '无更多数据',
            icon: 'none'  
          });
          console.log(that.onReachBottom);
          that.onReachBottom = function(){}
        }
      }
  })
},

 
  onLoad:function(options){
      this.getResults()
   

    
    // 页面初始化 options为页面跳转所带来的参数
  
   },
   onPullDownRefresh: function () {
    let that =this
    that.onLoad(that.options)
    wx.stopPullDownRefresh() //刷新完成后停止下拉刷新动效
  },
   onReady:function(){
  
    // 页面渲染完成
  
   },
  
   onShow:function(){
  
    // 页面显示
  
   },
  
   onHide:function(){
  
    // 页面隐藏
  
   },
  
   onUnload:function(){
  
    // 页面关闭
  
   },
   onShareAppMessage: function () {
    const { info, code } = that.data;
    return {
      title: info.name,
      path: `/pages/share/index?code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  },
  onShareTimeline: function(){
    const { info, code } = that.data;
    return {
      title: info.name,
      query: `code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  }

  
  })


